home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / RecordSet.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.2 KB  |  139 lines

  1. package symantec.itools.db.pro;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Hashtable;
  5. import java.util.Vector;
  6. import symantec.itools.db.net.BinaryInputStream;
  7. import symantec.itools.db.net.BinaryOutputStream;
  8. import symantec.itools.db.net.NetRecord;
  9. import symjava.sql.SQLException;
  10.  
  11. class RecordSet {
  12.    private RelViewPos _relPos;
  13.    private Hashtable _recTrees;
  14.    private boolean _allDataRead;
  15.  
  16.    RecordSet(RelViewPos relPos) {
  17.       this._relPos = relPos;
  18.       this._recTrees = new Hashtable();
  19.       this._allDataRead = false;
  20.    }
  21.  
  22.    synchronized void freeRecordTrees() {
  23.       Enumeration e = this._recTrees.elements();
  24.  
  25.       while(e.hasMoreElements()) {
  26.          RecordTree t = (RecordTree)e.nextElement();
  27.          if (t != null) {
  28.             t.freeRecordBlocks();
  29.          }
  30.       }
  31.  
  32.       this._recTrees.clear();
  33.       this._relPos = null;
  34.       this._allDataRead = false;
  35.    }
  36.  
  37.    RecordTree getRecordTree(RelViewPos pos) throws SQLException {
  38.       return this.getRecordTree(pos.getRecNum());
  39.    }
  40.  
  41.    NetRecord getRecord(int recNum) throws SQLException {
  42.       RecordTree t = this.getRecordTree(recNum);
  43.       return t.getRecord();
  44.    }
  45.  
  46.    synchronized void notifyRecChanged(NetRecord rec) throws SQLException {
  47.       if (this._relPos != null) {
  48.          this._relPos.notifyServerOfDataChange(rec);
  49.       }
  50.  
  51.    }
  52.  
  53.    synchronized void deleteRecord(NetRecord rec) throws SQLException {
  54.       if (this._relPos != null) {
  55.          this._relPos.deleteServerRecord(rec, rec.getRecordNum());
  56.       }
  57.  
  58.    }
  59.  
  60.    boolean isRecordTreeStored(int recNum) {
  61.       return this._recTrees.containsKey(new Integer(recNum));
  62.    }
  63.  
  64.    void storeRecordTree(RecordTree t) {
  65.       NetRecord r = t.getRecord();
  66.       if (!this.isRecordTreeStored(r.getRecordNum())) {
  67.          this._recTrees.put(new Integer(r.getRecordNum()), t);
  68.       }
  69.  
  70.    }
  71.  
  72.    synchronized NetRecord getNewRecord() throws SQLException {
  73.       if (this._relPos != null) {
  74.          NetRecord r = this._relPos.getNewServerRecord();
  75.          RecordTree t = new RecordTree(r);
  76.          this.storeRecordTree(t);
  77.          return r;
  78.       } else {
  79.          return new NetRecord();
  80.       }
  81.    }
  82.  
  83.    synchronized void undoRecord(NetRecord rec) throws SQLException {
  84.       if (this._relPos != null) {
  85.          this._relPos.undoServerRecord(rec, rec.getRecordNum());
  86.       }
  87.  
  88.    }
  89.  
  90.    synchronized BinaryInputStream getRemoteInputStream(int recID, int colIndex) throws SQLException {
  91.       if (this._relPos != null) {
  92.          return this._relPos.getRemoteInputStream(recID, colIndex);
  93.       } else {
  94.          throw new SQLException("Invalid Record block.");
  95.       }
  96.    }
  97.  
  98.    BinaryOutputStream getRemoteOutputStream(int recID, int colIndex) throws SQLException {
  99.       if (this._relPos != null) {
  100.          return this._relPos.getRemoteOutputStream(recID, colIndex);
  101.       } else {
  102.          throw new SQLException("Invalid Record block.");
  103.       }
  104.    }
  105.  
  106.    private synchronized RecordTree getRecordTree(int recNum) throws SQLException {
  107.       NetRecord r = null;
  108.       RecordTree t = null;
  109.       if (this._relPos == null) {
  110.          return new RecordTree();
  111.       } else if (recNum == 0) {
  112.          return new RecordTree();
  113.       } else if (this.isRecordTreeStored(recNum)) {
  114.          return (RecordTree)this._recTrees.get(new Integer(recNum));
  115.       } else if (this._allDataRead) {
  116.          return new RecordTree();
  117.       } else {
  118.          int rowsToRequest = this._relPos.getRowsToRequest();
  119.          int reqNum = recNum - this._recTrees.size() > rowsToRequest ? recNum - this._recTrees.size() : rowsToRequest;
  120.          Vector results = this._relPos.fetchRecords(this._recTrees.size() + 1, reqNum);
  121.  
  122.          for(int i = 0; i < results.size(); ++i) {
  123.             r = (NetRecord)results.elementAt(i);
  124.             t = new RecordTree(r);
  125.             this.storeRecordTree(t);
  126.             if (r.getState() == 105) {
  127.                this._allDataRead = true;
  128.             }
  129.          }
  130.  
  131.          if (this.isRecordTreeStored(recNum)) {
  132.             return (RecordTree)this._recTrees.get(new Integer(recNum));
  133.          } else {
  134.             return new RecordTree();
  135.          }
  136.       }
  137.    }
  138. }
  139.